﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form2 : Form
    { // 自定义坐标系参数
        private const int CustomMinX = 30;
        private const int CustomMaxX = 360;
        private const int CustomMinY = 0;
        private const int CustomMaxY = 255;
        private const int CustomInterval = 10; // X轴和Y轴等分为10份

        public Form2()
        {
            InitializeComponent();
            // 添加 Panel 控件的点击事件处理程序
            panel1.MouseClick += Panel1_MouseClick;

        }

        // 绘制坐标系
        private void DrawCoordinateSystem(PaintEventArgs e)
        {
            using (Graphics g = e.Graphics)
            {                // 获取 Panel 控件的尺寸
                int width = panel1.Width;
                int height = panel1.Height;

                // 清除 Panel 控件
                g.Clear(Color.White);

                // 绘制X轴
                g.DrawLine(Pens.Black, 30, height, width, height); // 考虑X轴偏移量
                                                                   // 绘制Y轴
                g.DrawLine(Pens.Black, 30, height, 30, 0); // 考虑X轴偏移量

                // 计算X轴刻度间隔
                float xInterval = (width - 30) / (float)(CustomInterval - 1); // 考虑X轴偏移量

                // 绘制X轴刻度
                for (int i = 0; i < CustomInterval; i++)
                {
                    float x = 30 + i * xInterval; // 考虑X轴偏移量
                    g.DrawLine(Pens.Black, x, height - 5, x, height + 5);
                    g.DrawString((CustomMinX + i * (CustomMaxX - CustomMinX) / (CustomInterval - 1)).ToString(), DefaultFont, Brushes.Black, x, height + 5);
                }

                // 计算Y轴刻度间隔
                float yInterval = height / (float)(CustomInterval - 1);

                // 绘制Y轴刻度
                for (int i = 0; i < CustomInterval; i++)
                {
                    float y = height - i * yInterval;
                    g.DrawLine(Pens.Black, 25, y, 35, y); // 考虑X轴偏移量
                    g.DrawString((CustomMinY + i * (CustomMaxY - CustomMinY) / (CustomInterval - 1)).ToString(), DefaultFont, Brushes.Black, -30, y);
                }
            }
        }

        // 绘制指定线段
        private void DrawCustomLine()
        {
            using (Graphics g = panel1.CreateGraphics())
            {
                // 定义线段坐标
                Point[] customPoints = new Point[]
                {
                    new Point(230, 0),
                    new Point(280, 20),
                    new Point(300, 70),
                    new Point(315, 90)
                };

                // 转换自定义坐标为屏幕坐标
                Point[] screenPoints = new Point[customPoints.Length];
                for (int i = 0; i < customPoints.Length; i++)
                {
                    Console.WriteLine($"自定义坐标为{customPoints[i].X}，转化后坐标为{MapXToScreen(customPoints[i].X, panel1.Width)}");
                    screenPoints[i] = new Point(MapXToScreen(customPoints[i].X, panel1.Width), MapYToScreen(customPoints[i].Y, panel1.Height));
                }

                // 绘制线段
                g.DrawLines(Pens.Red, screenPoints);
            }
        }

        // 将自定义X坐标映射到屏幕坐标
        private int MapXToScreen(int customX, int panelWidth)
        {
            return (int)((float)customX / (CustomMaxX - CustomMinX) * panelWidth+(float)customX);
        }

        // 将自定义Y坐标映射到屏幕坐标
        private int MapYToScreen(int customY, int panelHeight)
        {
            return (int)((float)(CustomMaxY - customY) / (CustomMaxY - CustomMinY) * panelHeight);
        }

        // Panel控件的点击事件处理程序
        private void Panel1_MouseClick(object sender, MouseEventArgs e)
        {
            // 将屏幕坐标映射到自定义坐标系中
            int customX = MapScreenToX(e.X, panel1.Width);
            int customY = MapScreenToY(e.Y, panel1.Height);

            MessageBox.Show($"点击坐标：X={e.X}, Y={e.Y}\n对应自定义坐标：X={customX}, Y={customY}");
        }

        // 将屏幕X坐标映射到自定义坐标
        private int MapScreenToX(int screenX, int panelWidth)
        {
            return (int)((float)screenX / panelWidth * (CustomMaxX - CustomMinX));
        }

        // 将屏幕Y坐标映射到自定义坐标
        private int MapScreenToY(int screenY, int panelHeight)
        {
            return CustomMaxY - (int)((float)screenY / panelHeight * (CustomMaxY - CustomMinY));
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            // 绘制坐标系
            DrawCoordinateSystem(e);
            // 绘制指定线段
            DrawCustomLine();
        }
    }
}
